﻿#ifndef CYXVALUEEDITPLUGIN_H
#define CYXVALUEEDITPLUGIN_H

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QLineEdit>
#include <QPushButton>
#include <QListWidget>
#include <QCheckBox>
#include <QDebug>
#include <QString>

#include <shlib/viewmodel/ctableeditplugin.h>
#include <shlib/quc_sdk/iconhelper/cplaticon.h>



class UiValueMapEdit : public QWidget {
    Q_OBJECT

public:
    UiValueMapEdit(int v, QString desc, CPlatIcon icon, QWidget* parent = nullptr) : QWidget(parent),
        m_valuebox(new QSpinBox(this)),
        m_descbox(new QLineEdit(this)),
        m_iconbtn(new QPushButton(this)),
        mainLayout(new QHBoxLayout(this)),
        m_checkValidBox(new QCheckBox(this))
    {

        // 设置初始值
        m_valuebox->setValue(v);
        m_descbox->setText(desc);
        m_iconbtn->setIcon(QIcon(icon.getPix()));

        value = v;
        descr = desc;
        iconpath = icon;

        mainLayout->addWidget(m_checkValidBox);
        mainLayout->addWidget(m_valuebox);
        mainLayout->addWidget(m_descbox);
        mainLayout->addWidget(m_iconbtn);

        // 连接信号和槽
        connect(m_valuebox, &QSpinBox::valueChanged, this, [&](int v){
            this->value = v;
        });

        connect(m_descbox, &QLineEdit::textChanged, this, [&](const QString& desc){
            this->descr = desc;
        });
    }

    ~UiValueMapEdit() {
        // 通常不需要显式删除这些指针，因为它们是子对象，会在父对象（QWidget）被销毁时自动删除
        // 但如果你在其他地方以某种方式将这些指针设置为非子对象（例如，通过 setParent(nullptr)），
        // 你需要确保在析构函数中删除它们以避免内存泄漏。
    }

    bool checkState() {
        return m_checkValidBox->checkState() == Qt::Checked ? true : false;
    }

public:

    QHBoxLayout* mainLayout;
    QSpinBox* m_valuebox;
    QLineEdit* m_descbox;
    QPushButton* m_iconbtn;
    QCheckBox* m_checkValidBox;

    int value;
    QString descr;
    CPlatIcon iconpath;
};
class CYxValueEditPlugin : public CTableEditPlugin
{
    Q_OBJECT
public:
    CYxValueEditPlugin(QWidget* parent = nullptr);
    virtual bool parseData(QString data)override;
    virtual QString readData()override;
    virtual bool checkData()override;
    int vcount();

private:
    QJsonObject root;
    QString m_str;

private:
    QVBoxLayout* mainLayout = nullptr;
    QPushButton* m_addbtn = nullptr;
    QPushButton* m_delbtn = nullptr;

    QMap<UiValueMapEdit*, UiValueMapEdit*> m_items;
};

#endif // CYXVALUEEDITPLUGIN_H
